package com.psy.counseling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.psy.counseling.entity.Ask;
import com.psy.counseling.entity.Schedule;
import com.psy.counseling.mapper.ScheduleMapper;
import com.psy.counseling.service.ScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psy.counseling.vo.DeleteVO;
import com.psy.counseling.vo.PageVO;
import com.psy.counseling.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 梁学儒
 * @since 2022-05-31
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
    @Resource
    private ScheduleMapper scheduleMapper;

    private static final Integer M = 1;

    private static final Integer S = 7;

    /**
     * 添加值班
     * @param schedule
     * @return ResultVo
     */
    @Transactional
    public ResultVo addSchedule(Schedule schedule){
        if(ObjectUtils.isEmpty(schedule.getUseId())){
            return ResultVo.builder().code("400").msg("插入失败!").data("用户不能为空").build();
        }else if(ObjectUtils.isEmpty(schedule.getStart())||ObjectUtils.isEmpty(schedule.getEnd())){
            return ResultVo.builder().code("400").msg("插入失败!").data("时间不能为空").build();
        }else if(ObjectUtils.isEmpty(schedule.getDay())||schedule.getDay()<M||schedule.getDay()>S){
            return ResultVo.builder().code("400").msg("插入失败!").data("日期不合法").build();
        }else if(scheduleMapper.insert(schedule)>0){
            return ResultVo.builder().code("200").msg("插入成功！").data(schedule).build();
        }else {
            return ResultVo.builder().code("500").msg("插入失败！").data("系统错误").build();
        }
    }

    /**
     * 删除值班
     * @param deleteVO
     * @return ResultVo
     */
    @Transactional
    public ResultVo deleteSchedule(DeleteVO deleteVO){
        if(ObjectUtils.isNotEmpty(deleteVO.getId())){
            if(this.removeById(deleteVO.getId())){
                return ResultVo.builder().code("200").msg("删除成功！").data(null).build();
            }
        }else if(ObjectUtils.isNotEmpty(deleteVO.getIds())){
            if(this.removeByIds(deleteVO.getIds())){
                return ResultVo.builder().code("200").msg("删除成功！").data(null).build();
            }
        }else {
            return ResultVo.builder().code("400").msg("删除失败！").data("id不能为空").build();
        }
        return ResultVo.builder().code("500").msg("删除失败！").data("系统错误").build();
    }

    /**
     * 查询值班
     * @param schedule
     * @param page
     * @return ResultVo
     */
    @Transactional
    public ResultVo listSchedules(Schedule schedule, PageVO page) {
        Page<Schedule> schedulePage = new Page<>(page.getPageNum(),page.getPageSize());
        LambdaQueryWrapper<Schedule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(schedule.getDay()),Schedule::getDay,schedule.getDay())
                .eq(ObjectUtils.isNotEmpty(schedule.getUseId()),Schedule::getUseId,schedule.getUseId())
                .eq(ObjectUtils.isNotEmpty(schedule.getStart()),Schedule::getStart,schedule.getStart())
                .eq(ObjectUtils.isNotEmpty(schedule.getEnd()),Schedule::getEnd,schedule.getEnd());
        schedulePage = this.page(schedulePage,queryWrapper);
        return ResultVo.builder().code("200").msg("查询成功！").data(schedulePage).build();
    }


    /**
     * 查询值班
     * @param userId
     * @return List<Schedule>
     */
    @Transactional
    public List<Schedule> getScheduleList(Long userId) {
        QueryWrapper<Schedule> SQW = new QueryWrapper<>();
        SQW.eq("useId", userId);
        return scheduleMapper.selectList(SQW);
    }

    /**
     * 修改值班
     * @param schedule
     * @return ResultVo
     */
    @Transactional
    public ResultVo updateSchedule(Schedule schedule){
        if(ObjectUtils.isNotEmpty(schedule)){
            QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
            wrapper.eq("id",schedule.getId());
            Schedule schedule1 = scheduleMapper.selectById(schedule.getId());
            if(ObjectUtils.isNotEmpty(schedule.getStart())){
                schedule1.setStart(schedule.getStart());
                log.debug(schedule.getStart());
            }
            if(ObjectUtils.isNotEmpty(schedule.getEnd())){
                schedule1.setEnd(schedule.getEnd());
            }
            boolean update = this.update(schedule1,wrapper);
            if(update){
                return ResultVo.builder().code("200").msg("修改成功！").data(schedule1).build();
            }else {
                return ResultVo.builder().code("500").msg("修改失败！").data("系统错误").build();
            }
        }else {
            return ResultVo.builder().code("400").msg("修改失败！").data("对象不能为空").build();
        }
    }

}
